package com.me.jingdongshop.ui.activity;

import android.os.CountDownTimer;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.me.common.base.BaseActivity;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.LimitbuyBean;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.TestFragmentPre;
import com.me.jingdongshop.ui.adapter.HomeFalshSaleAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by wuer on 2017/5/6/006.
 * 限时抢购
 */
public class HomeFlashSaleActivity extends BaseActivity {
    private ImageView ivHeadBack;
    private ImageView ivHeadCart;
    private TextView tvHomeFlashSaleTime;
    private RecyclerView recyclerView;
    private HomeFalshSaleAdapter homeFalshSaleAdapter;
    private LimitbuyBean homeFlashSale;
    private List<LimitbuyBean.ProductListBean> myProductListBean;
    private CountDownTimer timer;
    @Override
    public int getLayoutRes() {
        return R.layout.activity_home_flash_sale;
    }

    @Override
    public void initView() {
        ivHeadBack = (ImageView) findViewById(R.id.iv_head_back);
        ivHeadCart = (ImageView) findViewById(R.id.iv_head_cart);
        //倒计时
        tvHomeFlashSaleTime = (TextView) findViewById(R.id.tv_home_flash_sale_time);
        timer = new CountDownTimer(7200000,1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
                hms.setTimeZone(TimeZone.getTimeZone("GMT"));
                Date tTime = new Date(millisUntilFinished);//跑步用时
                String totalTime = hms.format(tTime);
                tvHomeFlashSaleTime.setText(totalTime);

            }

            @Override
            public void onFinish() {
                tvHomeFlashSaleTime.setText("onfinish");
                timer.cancel();
            }
        };
        timer.start();

        //返回键
        ivHeadBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                   finish();
                }

        });

    }

    @Override
    public void initListener() {

    }

    TestFragmentPre mTestFragmentPre;

    @Override
    public void initData() {
        mTestFragmentPre = new TestFragmentPre(this);
        mTestFragmentPre.getLimitbuyData();
    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType == IHttpService.HTTP_LIMITBUY) {
            homeFlashSale = (LimitbuyBean) msg.obj;
            myProductListBean = homeFlashSale.getProductList();
            List<List<LimitbuyBean.ProductListBean>> list = new ArrayList<>();
            list.add(myProductListBean);
            //初始化RecyclerView
            initRecyclerView();
        }
    }

    private void initRecyclerView() {
        recyclerView = findView(R.id.rv_home_flash_sale);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        homeFalshSaleAdapter = new HomeFalshSaleAdapter(myProductListBean);
        recyclerView.setAdapter(homeFalshSaleAdapter);
        homeFalshSaleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
